package com.controller;
import java.util.Date;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.dao.dao.ICommentGoodsDao;
import com.dao.dao.ICommentSellerDao;
import com.dao.dao.IOrderDao;
import com.dao.dao.IOrderGoodsDao;
import com.dao.entity.CommentGoods;
import com.dao.entity.CommentSeller;
import com.dao.entity.Order;
import com.dao.entity.OrderGoods;
import com.dao.redis.IRedislLoginVerifyDao;
import com.net.base.BaseRequest;
import com.net.base.BaseResponse;
import com.util.NetUtil;
import com.util.Utils;
/**
* @author wen
*/
@Controller
@RequestMapping(value = "/comment")
public class CommentController {
	
	@Autowired
	IRedislLoginVerifyDao redislLoginVerifyDao;
	@Autowired
	ICommentGoodsDao commentGoodsDao;
	@Autowired
	ICommentSellerDao commentSellerDao;
	@Autowired
	IOrderGoodsDao orderGoodsDao;
	@Autowired
	IOrderDao orderDao;
	/**
	 * 查询物品评价，传入offset和limit
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/queryComment")
	@ResponseBody
	public BaseResponse<List<CommentGoods>> showGoods(HttpServletRequest request,@RequestBody BaseRequest<Map<String, Integer>> requestBody) {
		
		if (requestBody == null) {
			return NetUtil.getCommonFailuerResponse("请求数据为空");
		}
		Map<String, Integer> mapRequest = requestBody.getBody();
		if (mapRequest == null) {
			return NetUtil.getCommonFailuerResponse("请求数据为空");
		}
		String userName = Utils.getShopnameBytaken(redislLoginVerifyDao,
				request.getHeader("JSESSIONID"));
		if (userName == null) {
			return NetUtil.getCommonFailuerResponse("会话过期");
		}
		List<CommentGoods> listResult = commentGoodsDao.selectByGoodsid(mapRequest.get("id"),
				mapRequest.get("offset"), mapRequest.get("limit"));
		return NetUtil.getDataResponse(listResult, "数据请求成功");
	}
	
	
	/**
	 * 查询物品评价，传入offset和limit
	 * 
	 * @param request
	 * @return
	 */
	@RequestMapping(value = "/querySellerComment")
	@ResponseBody
	public BaseResponse<List<CommentSeller>> querySellerComment(HttpServletRequest request,@RequestBody BaseRequest<Map<String, String>> requestBody) {
		
		if (requestBody == null) {
			return NetUtil.getCommonFailuerResponse("请求数据为空");
		}
		Map<String, String> mapRequest = requestBody.getBody();
		if (mapRequest == null) {
			return NetUtil.getCommonFailuerResponse("请求数据为空");
		}
		String userName = Utils.getShopnameBytaken(redislLoginVerifyDao,
				request.getHeader("JSESSIONID"));
		if (userName == null) {
			return NetUtil.getCommonFailuerResponse("会话过期");
		}
		List<CommentSeller> listResult = commentSellerDao.selectByShopName(mapRequest.get("shopname"),
				Integer.parseInt(mapRequest.get("offset")), Integer.parseInt(mapRequest.get("limit")));
		return NetUtil.getDataResponse(listResult, "数据请求成功");
	}
	
	
	@RequestMapping(value = "/addGoodsComment")
	@ResponseBody
	public BaseResponse<Object> addGoodsComment(HttpServletRequest request,
			@RequestBody BaseRequest<CommentGoods> requestBody) {
		if (requestBody == null) {
			return NetUtil.getCommonFailuerResponse("请求数据为空");
		}
		CommentGoods commentGoods = NetUtil.getBodyFromBaseRequest(requestBody,
				CommentGoods.class);
		if (commentGoods == null) {
			return NetUtil.getCommonFailuerResponse("请求数据为空");
		}
		String userName = Utils.getUserNameBytaken(redislLoginVerifyDao,
				request.getHeader("JSESSIONID"));
		if (userName == null) {
			return NetUtil.getCommonFailuerResponse("会话过期");
		}
		//最好用事务
		//对订单物品评价即对物品评价,praise字段无用，物品订单id存在praise中用于修改物品订单是否评价
		Integer goodsId = commentGoods.getPraise();
		OrderGoods orderGoods = orderGoodsDao.selectByPrimaryKey(goodsId);
		orderGoods.setCommentstatus(true);
		orderGoodsDao.updateByPrimaryKeySelective(orderGoods);
		commentGoods.setTime(new Date());
		return NetUtil.returnResponseByDbCode(commentGoodsDao.insert(commentGoods));
	}
	
	@RequestMapping(value = "/addSellerComment")
	@ResponseBody
	public BaseResponse<Object> addSellerComment(HttpServletRequest request,
			@RequestBody BaseRequest<CommentSeller> requestBody) {
		if (requestBody == null) {
			return NetUtil.getCommonFailuerResponse("请求数据为空");
		}
		CommentSeller commentSeller = NetUtil.getBodyFromBaseRequest(requestBody,
				CommentSeller.class);
		if (commentSeller == null) {
			return NetUtil.getCommonFailuerResponse("请求数据为空");
		}
		String userName = Utils.getUserNameBytaken(redislLoginVerifyDao,
				request.getHeader("JSESSIONID"));
		if (userName == null) {
			return NetUtil.getCommonFailuerResponse("会话过期");
		}
		//最好用事务
		//对订单评价即对商家评价,praise字段无用，订单id存在praise中用于修改订单是否评价
		Integer orderId = commentSeller.getPraise();
		Order order = orderDao.selectByPrimaryKey(orderId);
		order.setCommentstatus(true);
		order.setState(4);
		orderDao.updateByPrimaryKeySelective(order);
		commentSeller.setTime(new Date());
		return NetUtil.returnResponseByDbCode(commentSellerDao.insert(commentSeller));
	}
}